<?php
/**
 * ViewAssign.php
 * 
 * @category   Naneau
 * @package    Naneau_Plugin
 * @copyright  Copyright (c) 2007 Maurice Fonk - http://naneau.nl
 * @version    0.1
 */

/** base class */
require_once 'Zend/Controller/Plugin/Abstract.php';

/**
 * Naneau_Controller_Plugin_ViewAssign
 * 
 * helper to assign variables to views
 *
 * @category   Naneau
 * @package    Naneau_Plugin
 * @copyright  Copyright (c) 2007 Maurice Fonk - http://naneau.nl
 */
class Naneau_Controller_Plugin_ViewAssign extends Zend_Controller_Plugin_Abstract {
    
    /**
     * the variables to assign
     *
     * @var array
     */
    private $_variables = array();

    /**
     * assign a variable
     *
     * @param string $key
     * @param mixed $val
     * @return Naneau_Controller_Plugin_ViewAssign
     */
    public function assign($key, $val) {
        $this->_variables[$key] = $val;
        return $this;
    }
    
    /**
     * overridden setter
     *
     * @param string $key
     * @param mixed $val
     */
    public function __set($key, $val) {
        $this->assign($key, $val);
    }

    /**
     * shutdown hook, does the actual assigning
     */
    public function dispatchLoopShutdown() {
        $viewRenderer = Zend_Controller_Action_HelperBroker::getExistingHelper('viewRenderer');
        //the infamous view renderer
        
        foreach ($this->_variables as $key => $val) {
            $viewRenderer->view->$key = $val;
        }
    }
}